package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description: 自定义编译时异常 loginIn方法需要用throws声明异常，然后交给loginIn方法调用者main方法来try-catch处理这个异常
 * User: lenovo
 * Date: 2022-07-16
 * Time: 9:34
 */
class UserNameException extends Exception{
    public UserNameException() {
    }

    public UserNameException(String message) {
        super(message);
    }
}
class PasswordException extends Exception{
    public PasswordException() {
    }

    public PasswordException(String message) {
        super(message);
    }
}
public class Login {
    private String userName = "abc";
    private String password = "123";

    public  void loginIn(String usName,String uspassword) throws UserNameException, PasswordException {
        if(!userName.equals(usName)){
            throw new UserNameException("用户名错误");

        }
        if(!password.equals(uspassword)){
            throw new PasswordException("密码错误");
        }
        System.out.println("登陆成功");
    }

    public static void main(String[] args) {
       try{
           Login login = new Login();
           login.loginIn("ad","12");
       }catch (UserNameException userNameException){
           userNameException.printStackTrace();
       }catch (PasswordException passwordException){
           passwordException.printStackTrace();
       }finally {
           System.out.println("finally这里代码一定执行");
       }
        System.out.println("其他代码逻辑");
    }
}
